package com.lemurian.bankdata.interceptor;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.lemurian.bankdata.lite.logger.BankDataLogger;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CachingHeadersInterceptor extends AbstractInterceptor implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public static final String CONTEXT_PATH = "/BANKDATA";
    private BankDataLogger log = BankDataLogger.getLogger(CachingHeadersInterceptor.class.getSimpleName());

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        final ActionContext context = invocation.getInvocationContext();
        //log.info(":: ON CACHING HEADERS INTERCEPTOR "+invocation.getProxy().getActionName());
        HttpServletResponse response = (HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE);
        HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
            if (response != null) {
                response.setHeader("Cache-control", "no-cache, no-store");
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Expires", "1");
                log.info(":: CLEAR CACHE");
                response.addHeader("Pragma", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
                response.addHeader("Cache-Control", "must-revalidate");
                response.addHeader("Expires", "Mon, 29 Nov 2020 10:00:00 GMT");
                response.addDateHeader("Expires", 1);
            }

        return invocation.invoke();
    }
}
